#include "GFXUtil.h"

CTexture::CTexture()
{
	texture = NULL;
	m_renderDepthStencilSurface = NULL;
	m_renderSurface = NULL;
}

CTexture::~CTexture()
{
	Release();
}

int CTexture::getWidth() 
{ 
	return info.Width; 
};

int CTexture::getHeight() 
{ 
	return info.Height; 
};

RECT CTexture::getBounds()
{
	RECT rect = {0, 0, getWidth()-1, getHeight()-1};
	return rect;
}

bool CTexture::Load(std::string filename, IDirect3DDevice9* device, D3DCOLOR transcolor)
{
	//standard Windows return value
	HRESULT result;
	    
	//get width and height from bitmap file
	result = D3DXGetImageInfoFromFile(filename.c_str(), &info);
	if (result != D3D_OK) 	{
		texture = NULL;
		return 1;
	}

	//create the new CTexture by loading a bitmap image file
	D3DXCreateTextureFromFileEx( 
		device, //Direct3D device object
		filename.c_str(),      //bitmap filename
		info.Width,            //bitmap image width
		info.Height,           //bitmap image height
		1,                     //mip-map levels (1 for no chain)
		D3DPOOL_DEFAULT,       //the type of surface (standard)
		D3DFMT_UNKNOWN,        //surface format (default)
		D3DPOOL_DEFAULT,       //memory class for the CTexture
		D3DX_DEFAULT,          //image filter
		D3DX_DEFAULT,          //mip filter
		transcolor,            //color key for transparency
		&info,                 //bitmap file info (from loaded file)
		NULL,                  //color palette
		&texture );            //destination CTexture

	//make sure the bitmap textre was loaded correctly
	if (result != D3D_OK) 	{
		texture = NULL;
		return 0;
	}

	return 1;
}

void CTexture::Release()
{
	SAFE_RELEASE(texture);
	SAFE_RELEASE(m_renderSurface);
	SAFE_RELEASE(m_renderDepthStencilSurface);
}



bool CTexture::Create(int width,int height, IDirect3DDevice9* device)
{
	//if CTexture is already in use, first deallocate memory
	if (texture)
	{
		texture->Release();
		texture = NULL;
	}

	HRESULT r;
	r = D3DXCreateTexture(device, width, height, 1, 
        D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture );
	if (r != D3D_OK) {
		texture = NULL;
		return false;
	}

	//save CTexture info
	info.Width = width;
	info.Height = height;
	info.Format = D3DFMT_A8R8G8B8;

	//now that CTexture has been created, fill it with solid white
	//lock the CTexture
	D3DLOCKED_RECT sRect;
	texture->LockRect(0, &sRect, NULL, NULL);
	BYTE *bytes = (BYTE*)sRect.pBits;
		
	//clear CTexture to white
	memset(bytes, 255, info.Width*sRect.Pitch);

	//unlock the CTexture
	texture->UnlockRect(0);

	return true;
}

/**
	Saves a CTexture in memory to a file. Supports BMP, TGA, JPG, PNG, DDS. Default is BMP.
**/
bool CTexture::Save(std::string filename, _D3DXIMAGE_FILEFORMAT format)
{
	if (texture)
	{
		D3DXSaveTextureToFile(filename.c_str(), format, texture, NULL);
		return true;
	}
	else
		return false;
}

CSprite::CSprite()
{
	m_rotation = 0;
	m_color = 0xFFFFFFFF;
	m_width = 1;
	m_height = 1;

	m_position = D3DXVECTOR3(0,0,0);
	m_scale = D3DXVECTOR3(1,1,1);
	D3DXMatrixIdentity(&m_world);
}

void CSprite::setImage(std::shared_ptr<CTexture> image)
{
	m_image = image;
	this->setWidth(image->getWidth());
	this->setHeight(image->getHeight());
}

void CSprite::update(double deltaTime)
{
	transform();
}

void CSprite::transform()
{
	D3DXMatrixIdentity(&m_world);
	D3DXVECTOR2 scale(m_scale.x, m_scale.y);
	D3DXVECTOR2 center((float)(m_width*m_scale.x)/2, (float)(m_height*m_scale.y)/2); 
	D3DXVECTOR2 trans(m_position.x, m_position.y);
	D3DXMatrixTransformation2D(&m_world,NULL,0,&scale,&center,(float)m_rotation,&trans);
}

void CSprite::draw(ID3DXSprite* spriteHandler)
{
	RECT srcRect;
	srcRect.left = 0;
	srcRect.right = m_width;
	srcRect.top = 0;
	srcRect.bottom = m_height;

	spriteHandler->SetTransform(&m_world);
	spriteHandler->Draw(m_image->getTexture(),&srcRect,NULL,NULL,m_color);
	D3DXMATRIX ident;
	D3DXMatrixIdentity(&ident);
	spriteHandler->SetTransform(&ident);
}
